# -*- coding:utf-8 -*-
import pymongo

# 这个文件是独立运行的，不用scrapy命令，连接数据库，得到游戏的简要信息
mongo_client = pymongo.MongoClient("mongodb://user_spider:pq888888@192.168.136.128:27017/?authSource=spider")
# print(mongo_client)
mongo_db = mongo_client["spider"]
# print(mongo_db)
collist = mongo_db.list_collection_names()
# print(collist)
coll = mongo_db["MyabandonwareCategorfornameisnull"]
coll.create_index([("rate",-1)])#创建索引目的是为了减少资源的消耗，要不然下面的语句会崩
for n, i in enumerate(coll.find().sort([('rate', -1)])):
    GENER=''
    THEME=''
    if "GENRE" in i:
        for j in i["GENRE"]:
            GENER=GENER+' '+j.replace(" ","_")
    if "THEME" in i:
        for j in i["THEME"]:
            THEME=THEME+' '+j.replace(" ","_")
    PLATFORM=i["PLATFORM"].replace(" ","_")
    headHtmlUnit="""<div class="item %s %s %s">
                <div class=" c-style-origin ">
            <div class="c-for-picture ">
                <a class=" " href="./detail/%s.html" target="_blank">
                    <picture>""" %(PLATFORM.upper(),GENER.upper(),THEME.upper(),i["name"].replace('"',"_").replace("/","_").replace(":","_").replace("?","_").replace("*","_"))
    tailHtmlUnit=""" <img class="c-thumb__img " alt="%s" data-original="%s" />
                    </picture>
                </a>
            </div>
            <a class="c-item-game__name " href="./detail/%s.html" target="_blank">%s</a
        ><span class="c-item-game__platforms ">%s</span
        ><span class="c-item-game__year ">%s</span>
      </div>
    </div>""" %(i["name"],i["thumbSrc"],i["name"].replace('"',"_").replace("/","_").replace(":","_").replace("?","_").replace("*","_"),i["name"],PLATFORM,i["YEAR"])
    with open("../../output/dasdfsrqwrasf.html", encoding="utf-8", mode="a") as f:
        f.write(str(headHtmlUnit+tailHtmlUnit))
    print(PLATFORM,GENER,THEME,i["name"],i["thumbSrc"],i["YEAR"])
